Skip to content

feat(python): support FastAPI APIRouter prefixes#81

Closed
AsishKumarDalal wants to merge 1 commit into
openclaw:mainfrom
AsishKumarDalal:feat/fastapi-apirouter-prefixes
Closed

feat(python): support FastAPI APIRouter prefixes#81
AsishKumarDalal wants to merge 1 commit into
openclaw:mainfrom
AsishKumarDalal:feat/fastapi-apirouter-prefixes

Conversation

@AsishKumarDalal
Copy link
Copy Markdown
Contributor

Overview

When mapping FastAPI projects, clawpatch previously only mapped the route paths specified directly inside route decorators (e.g., @router.get("/items")). If the APIRouter was initialized with a path prefix (e.g., router = APIRouter(prefix="/api/v1")), the generated seeds would incorrectly display the route path as /items instead of the fully resolved /api/v1/items path.

This Pull Request resolves this limitation by introducing robust static analysis to extract, parse, and propagate APIRouter prefix declarations, seamlessly combining them with decorators to generate 100% accurate FastAPI routes.

Key Changes

  1. APIRouter Prefix Extraction (src/mappers/python.ts):
    • Implemented parseFastApiRouterPrefixes(source) which matches APIRouter( initializations and identifies the variable name it is assigned to.
    • Leverages a balanced parenthesis parser (findBalancedParenthesis) and top-level Python argument splitter (splitTopLevelPythonArgs) to securely extract the prefix value, even when other arguments are present or lines are formatted differently.
    • Robustly parses single/double quotes, raw/unicode prefixes, and unescapes literal values.
  2. FastAPI Path Combination Logic (combineFastApiPaths):
    • Added a safe path-merging utility that handles leading and trailing slashes gracefully. Ensures the returned route starts with a single / and has no duplicate internal slashes (e.g., /api/ + /items merges correctly to /api/items).
  3. Decorator Processing Integration:
    • Updated the FastAPI route parsing loop to lookup and apply any resolved router prefixes to the decorated function routes before adding them as feature seeds.

This high-value contribution drastically improves the quality and correctness of the Python mapper for modern FastAPI codebases.

@steipete
Copy link
Copy Markdown
Contributor

Thanks @AsishKumarDalal. I landed this on main in fb46ba9 after adding the maintainer-side changelog entry and regression coverage.

I could not push the fixups back to the PR branch because maintainer edits are disabled on the fork, so I closed this PR after landing the equivalent maintainer commit. For future PRs, enabling Allow edits by maintainers lets us preserve the normal PR update/merge path.

@steipete steipete closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants